package com.example.drive.net.interceptor

import android.util.Log
import okhttp3.FormBody
import okhttp3.Interceptor
import okhttp3.Response

/**
 * 通用拦截器
 */
class CommonInterceptor : Interceptor {
    companion object {
        const val TAG = "CommonInterceptor"
    }

    override fun intercept(chain: Interceptor.Chain): Response {
        // 请求
        val request = chain.request()
        Log.i(TAG, "request = $request")
        // 打印请求参数
        val requestBody = request.body
        if (requestBody is FormBody) {
            for (i in 0 until requestBody.size) {
                Log.i(TAG, "${requestBody.encodedName(i)} = ${requestBody.encodedValue(i)}")
            }
        }
        // 响应
        val response = chain.proceed(request)
        Log.i(TAG, "response = $response")
        return response
    }
}